package com.hdoit.ares.web.personnel.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.personnel.model.Personnel;
import com.hdoit.ares.logical.personnel.service.IPersonnelService;
import com.hdoit.ares.logical.viewmodel.personnel.PersonnelSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.logical.setting.service.IParamOptionService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.viewmodel.setting.ParamOptionSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月15日 10:09:24
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/personnel/personnel/")
public class PersonnelController extends BaseController {

	@Override
	public String getRoutePath() {
		return "areas/admin/personnel/personnel/";
	}

	private IPersonnelService personnelService;
	
	public IPersonnelService getPersonnelService() {
		return personnelService;
	}
	@Resource
	public void setPersonnelService(IPersonnelService personnelService) {
		this.personnelService = personnelService;
	}	
	private IRoleEmployeeService roleEmployeeService;	
	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private IParamOptionService paramOptionService;
	public IParamOptionService getParamOptionService() {
		return paramOptionService;
	}
	@Resource
	public void setParamOptionService(IParamOptionService paramOptionService) {
		this.paramOptionService = paramOptionService;
	}
	
	
	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "personnelDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";
	private final String provinceCityViewName = "provinceCity";
	
	
    @RequestMapping(value = provinceCityViewName, method = { RequestMethod.GET,RequestMethod.POST},produces="text/plain;charset=utf-8")
		@ResponseBody
		public String provinceCity(String keyId){
    	    ParamOptionSearch paramOptionSearch=new ParamOptionSearch();
    		
    	    if("all".equals(keyId)){
    			paramOptionSearch.setParamName("province");
    		}else{
    			paramOptionSearch.setParentId(keyId);
    			System.err.println("keyId是什么"+keyId);
    		}
	    	
	    	
	    	List<ParamOption> paramOptions=paramOptionService.queryList(paramOptionSearch);
	    	
	    	return JsonUtils.toJson(paramOptions);
	 }
	
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,PersonnelSearch search,boolean ajaxPost) {
		
		String employeeAccount = getLoginModel().getLoginAccount();
		System.out.println(employeeAccount);
		pagerModel.setTotalAmount(personnelService.queryCount(search));
		List<Personnel> personnels = personnelService.queryList(search, pagerModel, employeeAccount);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("personnels", personnels);
		model.addAttribute("personnelSearch", search);
		
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String projectDetailed(Model model,String key){
        Personnel personnel=new Personnel();
        personnel.setJoinUsDate(DateUtils.getCurrent());
        personnel.setSex(SexStatus.male);
        System.out.println("employeeId" + key);
        
        if(!StringUtils.isEmpty(key)){
        	personnel=personnelService.queryModel(key);
            List<String> roleIds = roleEmployeeService.queryRoleIdsByEmployeeId(key);
            model.addAttribute("roleIds",roleIds);
        }
        
        model.addAttribute("personnel", personnel);
       
        return view(detailedViewName);
    }

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Personnel personnel){
        if(StringUtils.isEmpty(personnel.getEmployeeId())){
        	personnel.setEmployeeId(CommonUtils.getKeyId());
        	personnel.setCreateDate(DateUtils.getCurrent());
        	personnel.setCreateUserId(getLoginModel().getLoginId());
        	personnel.setCreateUserName(getLoginModel().getLoginName());
        	personnel.setModelStatus(ModelStatus.add);       	
        }else{
        	personnel.setModifyDate(DateUtils.getCurrent());
        	personnel.setModifyUserId(getLoginModel().getLoginId());
        	personnel.setModifyUserName(getLoginModel().getLoginName());
        	personnel.setModelStatus(ModelStatus.update);
        }
        //markcode
        personnel.setMarkCode(ChineseUtils.getPinYinHeadChar(personnel.getEmployeeName()));
        
        ActionResult actionResult;
        if(personnelService.applyChange(personnel)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }
        model.addAttribute("personnel", personnel);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
        return view(detailedViewName);
    }
    
    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(personnelService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
	
}
